<?php 

namespace app\admin\event;

use app\common\enum\BaseEnum;
use app\common\model\Brand as BrandModel;
use app\common\utils\JsonService;

class BrandEvent{

    /*
    * 商品品牌列表
    */
    public static function brandList(){
        $params = array_filter(request()->param());
        $page = ! empty($params['page']) && $params['page'] >= 1 ? $params['page'] : 1;
        $where = [];
        if($id = isset($params['id'])){
            $where[] = [['id','=',$params['id']]];
        }
        if($name = isset($params['name'])){
            $where[] = [['name','like',"%{$params['name']}%"]];
        }

        if($cat_id = isset($params['cat_id'])){
            $where[] = [['cat_id','like',"%{$params['cat_id']}%"]];
        }

        $condition = [
            'with'  => ['getCategory'],
            'where' => $where,
            'order' => ['id' => 'desc'],
                'paginate' => [
                'page' => $page,
                'query' => $params,
            ],
        ];
        $brand = BrandModel::getPageAll($condition)->each(function($item){
            $item['is_hot'] = BaseEnum::getBoolValue($item['is_hot']);
            $item['cat_id'] = $item['getCategory']['name'];
        });
        return $brand;
    }


    /*
    * 新增商品品牌
    */
    public static function createBrand($data){

        try {
            BrandModel::create($data);
        } catch (\Exception $ex) {
            return JsonService::error($ex->getMessage());
        }
        return JsonService::success();
    }

    /*
    * 更新商品品牌
    */
    public static function updateBrand($data){

        try {
            BrandModel::updateOne($data, ['id' => $data['id']]);
        } catch (\Exception $ex) {
            return JsonService::error($ex->getMessage());
        }
        return JsonService::success();
    }


}

